package com.engine.kobelco.duss;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-10-31
 * @Description: 提供可给ERP调用的接口，可修改流程表单字段。
 * @ModifyLog:
 **/

public class WeaKobelcoWeb extends BaseBean {

    /**
     * @Description: 第三方调用推送付款申请-对公接口更新流程字段
     * @Author: Dusongsong
     * @Date: 2023-09-10
     */
    @POST
    @Path("/pushPublicFksqData")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String pushPublicFksqData(JSONObject paramsJson) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        String code = "SUCCESS";
        String data = "";
        String message = "推送成功";
        try {
            writeLog(">>>>>pushPubFksqData>>>paramsJson=" + paramsJson);
            RecordSet rs = new RecordSet();
            String tablename = "formtable_main_259";
            String tablenamedt1 = "formtable_main_259_dt1";
            KobelcoUtil kobelcoUtil = new KobelcoUtil();
            String requestid = Util.null2String(paramsJson.getString("requestid"));
            String mainid = kobelcoUtil.geIdByRequestid(tablename,requestid);
            String dataListStr = Util.null2String(paramsJson.getString("dataList"));
            if (kobelcoUtil.isJSONArray(dataListStr)) {
                JSONArray dataList = JSONArray.parseArray(dataListStr);
                int succcount = 0;
                int failcount = 0;
                JSONObject dataJson = new JSONObject();
                JSONArray returnDataList = new JSONArray();
                for (int i = 0; i < dataList.size(); i++) {
                    JSONObject returnJson = new JSONObject();
                    JSONObject dataItem = dataList.getJSONObject(i);
                    String mxid = Util.null2String(dataItem.getString("mxid"));
                    //提交银行状态
                    String yqzlzt = Util.null2String(dataItem.getString("yqzlzt"));
                    //银行处理状态
                    String yxclzt = Util.null2String(dataItem.getString("yxclzt"));
                    String sql = "update "+tablenamedt1+" set yqzlzt=?,yxclzt=? where mainid=? and id=?";
                    if(rs.executeUpdate(sql,yqzlzt,yxclzt,mainid,mxid)){
                        succcount++;
                    }else {
                        returnJson.put("mxid",mxid);
                        failcount++;
                    }
                    returnDataList.add(returnJson);
                }
                dataJson.put("succcount",succcount);
                dataJson.put("failcount",failcount);
                dataJson.put("totalcount",dataList.size());
                dataJson.put("item",returnDataList);
                data = dataJson.toJSONString();
            } else {
                code = "PARAMETER_FORMAT_ERROR";
                message = "JSON格式化错误，请检查参数:DATA";
            }
        } catch (Exception e) {
            code = "SYSTEM_INNER_ERROR";
            message = "程序内部异常";
        }
        apidatas.put("code", code);
        apidatas.put("data", data);
        apidatas.put("message", message);
        writeLog(">>>>>pushPubFksqData>>>apidatas=" + apidatas.toString());
        return JSONObject.toJSONString(apidatas);
    }
    /**
     * @Description: 第三方调用推送付款申请-对私接口更新流程字段
     * @Author: Dusongsong
     * @Date: 2023-09-10
     */
    @POST
    @Path("/pushPrivateFksqData")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String pushPrivateFksqData(JSONObject paramsJson) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        String code = "SUCCESS";
        String data = "";
        String message = "推送成功";
        try {
            writeLog(">>>>>pushPubFksqData>>>paramsJson=" + paramsJson);
            RecordSet rs = new RecordSet();
            String tablename = "formtable_main_271";
            String tablenamedt1 = "formtable_main_271_dt1";
            KobelcoUtil kobelcoUtil = new KobelcoUtil();
            String requestid = Util.null2String(paramsJson.getString("requestid"));
            String mainid = kobelcoUtil.geIdByRequestid(tablename,requestid);
            String dataListStr = Util.null2String(paramsJson.getString("dataList"));
            if (kobelcoUtil.isJSONArray(dataListStr)) {
                JSONArray dataList = JSONArray.parseArray(dataListStr);
                int succcount = 0;
                int failcount = 0;
                JSONObject dataJson = new JSONObject();
                JSONArray returnDataList = new JSONArray();
                for (int i = 0; i < dataList.size(); i++) {
                    JSONObject returnJson = new JSONObject();
                    JSONObject dataItem = dataList.getJSONObject(i);
                    String mxid = Util.null2String(dataItem.getString("mxid"));
                    //提交银行状态
                    String yqzlzt = Util.null2String(dataItem.getString("yqzlzt"));
                    //银行处理状态
                    String yxclzt = Util.null2String(dataItem.getString("yxclzt"));
                    String sql = "update "+tablenamedt1+" set yqzlzt=?,yxclzt=? where mainid=? and id=?";
                    if(rs.executeUpdate(sql,yqzlzt,yxclzt,mainid,mxid)){
                        succcount++;
                    }else {
                        returnJson.put("mxid",mxid);
                        failcount++;
                    }
                    returnDataList.add(returnJson);
                }
                dataJson.put("succcount",succcount);
                dataJson.put("failcount",failcount);
                dataJson.put("totalcount",dataList.size());
                dataJson.put("item",returnDataList);
                data = dataJson.toJSONString();
            } else {
                code = "PARAMETER_FORMAT_ERROR";
                message = "JSON格式化错误，请检查参数:DATA";
            }
        } catch (Exception e) {
            code = "SYSTEM_INNER_ERROR";
            message = "程序内部异常";
        }
        apidatas.put("code", code);
        apidatas.put("data", data);
        apidatas.put("message", message);
        writeLog(">>>>>pushPubFksqData>>>apidatas=" + apidatas.toString());
        return JSONObject.toJSONString(apidatas);
    }


}
